<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        :default-active="$route.path"
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img
              v-if="!userInfo.user_pic"
              src="../../assets/logo.png"
              alt=""
              class="avatar"
            />
            <img v-else :src="userInfo.user_pic" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="/user-info"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="/user-avatar"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="/user-pwd"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <!-- 退出登录弹出框和清除token -->
        <el-menu-item @click="logoutHandler" index="2"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <img v-if="!userInfo.user_pic" src="../../assets/logo.png" alt="" />
          <img v-else :src="userInfo.user_pic" alt="" />
          <span>欢迎 {{ userInfo.nickname || userInfo.username }}</span>
        </div>
        <div class="menu-box">
          <el-menu
            :default-active="$route.path"
            class="el-menu-vertical-demo"
            background-color="#23262e"
            text-color="#fff"
            active-text-color="rgb(64, 158, 255)"
            router
          >
            <!-- 不能在template上绑定key必须在真实dom上绑定key -->
            <template v-for="item in menus">
              <el-menu-item
                v-if="!item.children"
                :index="item.indexPath"
                :key="item.indexPath"
              >
                <i class="el-icon-setting"></i>
                <span slot="title">{{ item.title }}</span>
              </el-menu-item>
              <el-submenu v-else :index="item.indexPath" :key="item.indexPath">
                <template slot="title">
                  <i :class="item.icon"></i>
                  <span>{{ item.title }}</span>
                </template>
                <el-menu-item-group>
                  <template v-for="item in item.children">
                    <el-menu-item :key="item.indexPath" :index="item.indexPath">
                      <i :class="item.icon"></i>
                      <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                  </template>
                </el-menu-item-group>
              </el-submenu>
            </template>
          </el-menu>
        </div>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
export default {
  namespaced: true,
  name: 'Main',
  data () {
    return {
      menus: {}
    }
  },
  created () {
    // this.$http在vue的原型树上
    // 在main中获取用户信息
    this.$store.dispatch('getUserInfo')
    this.getMenus()
    // console.log(this.$route.path)
  },
  methods: {
    logoutHandler () {
      this.$confirm('您确认退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.commit('updateToken', '')
          this.$message({
            type: 'success',
            message: '退出登录成功!'
          })
          this.$router.push('/login')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      // 清空vuex中的token
    },
    async getMenus () {
      const { data: res } = await this.$http.get(
        '/my/menus'
        //   , {headers: {
        //       Authorization: this.$store.state.token}
        //   }
      )
      //   console.log(res)
      if (res.code === 0) {
        this.menus = res.data
      }
    }
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
</style>
